//
// Created by LiuYou on 2021/5/9.
//

#include <cstdio>
#include <cstdlib>
#include <pwd.h>
#include <unistd.h>
#include <shadow.h>
#include <cstring>

/**
 * @brief 注意要使用 root 用户才能执行。
 * @param argc
 * @param argv
 * @return
 */
int main(int argc, char** argv) {

    if (argc < 2) {
        std::fprintf(stderr, "Usage...");
        std::exit(1);
    }


    char* inputPassword = getpass("Password: ");

    struct spwd* shadowLine = getspnam(argv[1]);

//    ====================[ Build | FileSystemTest07 | Debug ]========================
//    /usr/bin/cmake --build /home/lucas/CLionProjects/LinuxEnvironmentProgramming/cmake-build-debug --target FileSystemTest07 -- -j 6
//    [ 50%] Linking CXX executable FileSystemTest07
//    CMakeFiles/FileSystemTest07.dir/Test/FileSystem/FileSystemTest07-chkpass.cpp.o: In function `main':
//                                                                                                     /home/lucas/CLionProjects/LinuxEnvironmentProgramming/Test/FileSystem/FileSystemTest07-chkpass.cpp:24: undefined reference to `crypt'
//    collect2: error: ld returned 1 exit status
//    gmake[3]: *** [CMakeFiles/FileSystemTest07.dir/build.make:84: FileSystemTest07] Error 1
//    gmake[2]: *** [CMakeFiles/Makefile2:68: CMakeFiles/FileSystemTest07.dir/all] Error 2
//    gmake[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/FileSystemTest07.dir/rule] Error 2
//    gmake: *** [Makefile:118: FileSystemTest07] Error 2
// TODO 注意: 这里要手动链接 -lcrypt 。

    char* cryptedPassword = crypt(inputPassword, shadowLine->sp_pwdp);
    if (std::strcmp(shadowLine->sp_pwdp, cryptedPassword) == 0) {
        std::puts("OK!");
    } else {
        std::puts("Failure!");
    }

    return 0;
}